//
//	NSEC3HashTest.m
//	Tests
//
//	Created by Joey Deng on 1/29/20.
//

#import <XCTest/XCTest.h>
#import "dnssec_v2_crypto.h"
#include "mDNSEmbeddedAPI.h"
#if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)


@interface NSEC3HashTest : XCTestCase

@end

@implementation NSEC3HashTest

- (void)setUp {
	// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
	// Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testNSEC3Hash {
	const char * inputs[] = {
		"f", "fo", "foo", "foob", "fooba", "foobar"
	};
	mDNSu32 iterations[] = {
		150, 500, 2500
	};
	const char * salts[] = {
		"", "s", "salt"
	};
	const mDNSu8 outputs[][7][20] = {
		{
			{0xd9, 0xcc, 0x35, 0x41, 0xe6, 0x9c, 0x3b, 0xb5, 0x27, 0x10, 0x3d, 0xa9, 0xad, 0x25, 0xdf, 0x9e, 0x64, 0x9e, 0x97, 0x24},
			{0x85, 0x7f, 0x81, 0x5d, 0xe5, 0xe3, 0xd3, 0xfc, 0x0f, 0xe0, 0xca, 0x0d, 0xb1, 0xae, 0xa1, 0xad, 0x79, 0x48, 0x66, 0x49},
			{0xff, 0x84, 0x51, 0x8d, 0x22, 0x44, 0x52, 0x93, 0x02, 0x80, 0x62, 0x4f, 0x58, 0x3f, 0xd3, 0x79, 0x41, 0x1f, 0x8d, 0xed},
			{0xfb, 0x8a, 0xb8, 0x70, 0x23, 0xa6, 0x49, 0x49, 0x0f, 0xb6, 0x1a, 0x78, 0x8a, 0xb0, 0x4b, 0xf4, 0x08, 0x9a, 0x37, 0x47},
			{0x8a, 0xe3, 0xeb, 0x10, 0x3c, 0xc3, 0x8d, 0x79, 0x42, 0x92, 0x66, 0x7c, 0x65, 0x89, 0xf1, 0x33, 0x6b, 0x36, 0x00, 0x96},
			{0x34, 0xb0, 0xbc, 0xb7, 0xd3, 0xbb, 0x17, 0x8d, 0xd9, 0x08, 0x62, 0x22, 0x37, 0xcd, 0x1c, 0x6c, 0xb6, 0xac, 0x0d, 0x8b},
		},
		{
			{0xa5, 0xda, 0x58, 0x38, 0xc6, 0x5e, 0x37, 0xc9, 0x3c, 0xd7, 0x01, 0xf9, 0xaa, 0xd3, 0x5d, 0x93, 0x15, 0x41, 0x25, 0x71},
			{0xf2, 0x2a, 0xae, 0x68, 0x4c, 0xbb, 0x53, 0xfc, 0xd8, 0x4a, 0x37, 0x9a, 0xc7, 0xac, 0x1a, 0x8d, 0x0e, 0x43, 0xa6, 0x0e},
			{0x86, 0xc4, 0x41, 0x10, 0x8d, 0xd7, 0xfb, 0x91, 0xe7, 0x7f, 0xfe, 0xda, 0xcc, 0x05, 0x00, 0x24, 0x8d, 0xdb, 0xed, 0xcd},
			{0x99, 0x88, 0x17, 0x3c, 0xaf, 0x86, 0x15, 0xcb, 0x1c, 0x11, 0xec, 0xe7, 0xa4, 0xa6, 0x27, 0x33, 0xb7, 0x77, 0x7b, 0x89},
			{0x74, 0x10, 0xdd, 0x9f, 0xc0, 0x3c, 0x56, 0x5e, 0x14, 0x41, 0x3e, 0xa5, 0xa7, 0x7b, 0x18, 0xee, 0xd1, 0x11, 0x77, 0x46},
			{0x62, 0xd1, 0xea, 0x1d, 0x7a, 0x71, 0x33, 0xb3, 0x24, 0xd5, 0x16, 0xbd, 0x96, 0xac, 0xab, 0xcd, 0x5b, 0x7e, 0x6a, 0x5e},
		},
		{
			{0x8f, 0xd5, 0x78, 0xd2, 0x2c, 0x0f, 0x86, 0xa3, 0x68, 0x05, 0x73, 0x38, 0xbd, 0xa4, 0xc7, 0xf8, 0x8e, 0x27, 0xd7, 0xe8},
			{0x27, 0xcc, 0x83, 0xc5, 0xde, 0x5f, 0xe2, 0x5a, 0xe4, 0xdb, 0x74, 0xf7, 0xf4, 0x07, 0xea, 0x17, 0xea, 0x39, 0x32, 0x39},
			{0x0f, 0x1b, 0xa1, 0x22, 0xbd, 0x1f, 0xcf, 0x41, 0xc1, 0xa5, 0x97, 0x84, 0xf4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 0xb4, 0x6c, 0x37, 0x64, 0xea, 0x79, 0x67, 0x3c, 0x84},
		},
		{
			{0xa6, 0x89, 0x2f, 0x06, 0xba, 0xbb, 0x9f, 0x2d, 0x59, 0xb5, 0x29, 0x66, 0x41, 0xf8, 0xa0, 0x2c, 0x9e, 0x16, 0x3b, 0x02},
			{0x78, 0xe2, 0x40, 0x66, 0xdc, 0xfe, 0x85, 0x05, 0xa7, 0xd4, 0x9e, 0xa2, 0x92, 0x56, 0x4a, 0xab, 0x50, 0x0a, 0x1e, 0x97},
			{0xdb, 0x45, 0x13, 0x7a, 0xf1, 0x1f, 0x03, 0xc8, 0x2f, 0x8a, 0x5c, 0xe4, 0x34, 0xf0, 0x5b, 0x83, 0xd2, 0xa9, 0x4e, 0xa6},
			{0x0b, 0x35, 0x30, 0x8f, 0xef, 0x8e, 0x94, 0x5f, 0xbe, 0x96, 0xf9, 0xbb, 0x8c, 0x44, 0x5f, 0x5e, 0x59, 0x69, 0xfb, 0x6f},
			{0x38, 0xd5, 0x6f, 0x4e, 0x50, 0x01, 0x4b, 0x5b, 0x37, 0x91, 0xd4, 0x79, 0xa0, 0x81, 0xee, 0x86, 0x4f, 0x84, 0xa8, 0x0f},
			{0xd2, 0x68, 0xc7, 0xf0, 0xb6, 0x6c, 0x93, 0x31, 0x7b, 0x3a, 0x77, 0x6a, 0x0a, 0xfe, 0xcc, 0x93, 0xd0, 0x8f, 0x99, 0xc6},
		},
		{
			{0x7c, 0x51, 0x00, 0xf6, 0x97, 0xd2, 0xff, 0x32, 0x4e, 0xbe, 0x49, 0xa5, 0x97, 0xec, 0xc9, 0xc9, 0x32, 0x2c, 0x65, 0xc2},
			{0xbe, 0x73, 0x19, 0x16, 0x7f, 0x91, 0x19, 0xb7, 0x65, 0x6d, 0x6d, 0x72, 0x96, 0x29, 0xba, 0x88, 0xe1, 0x42, 0xa0, 0x17},
			{0x56, 0x14, 0xba, 0xa0, 0x4b, 0xac, 0xbc, 0xca, 0xc7, 0xbf, 0x2d, 0x02, 0x9b, 0x67, 0xea, 0x50, 0x58, 0xbf, 0xf5, 0x7c},
			{0xab, 0xda, 0x1c, 0x5c, 0x7f, 0xdd, 0xa4, 0x6f, 0xc4, 0x87, 0x0f, 0xe6, 0x43, 0x6f, 0x83, 0x9a, 0x08, 0x0b, 0xd5, 0xe1},
			{0x6a, 0xe1, 0x4d, 0x10, 0xe8, 0xca, 0xe4, 0x31, 0x09, 0xb5, 0xae, 0x52, 0xe5, 0x33, 0xbd, 0x77, 0x8c, 0x22, 0x23, 0x11},
			{0x87, 0x85, 0x5c, 0x6b, 0x29, 0x81, 0x77, 0x2d, 0x8b, 0x50, 0xab, 0x5f, 0x56, 0x6a, 0x7b, 0xe0, 0x7d, 0x6d, 0xbd, 0x55},
		},
		{
			{0xd1, 0xb1, 0x24, 0xc3, 0xf1, 0x99, 0x41, 0x67, 0x38, 0xd8, 0x9e, 0xfc, 0xa5, 0x54, 0x57, 0xf4, 0x02, 0xb9, 0x6c, 0xf4},
			{0xd3, 0xd8, 0xce, 0xea, 0x8c, 0xf9, 0x6e, 0x60, 0xa5, 0x3c, 0x7e, 0x9e, 0xee, 0xbf, 0xd9, 0xe3, 0x18, 0xb7, 0x89, 0x61},
			{0x78, 0x78, 0xea, 0x90, 0x57, 0x8b, 0x9c, 0x8c, 0xa0, 0x9d, 0xe4, 0x03, 0xcb, 0xd6, 0x24, 0xf5, 0x12, 0x5b, 0x76, 0xda},
			{0xd5, 0xf1, 0x94, 0x18, 0xb0, 0x28, 0x83, 0x86, 0x8f, 0xc9, 0x7d, 0x08, 0x55, 0x66, 0x00, 0x42, 0x49, 0x4d, 0x79, 0x06},
			{0xd8, 0x72, 0x25, 0xd0, 0x6f, 0x9e, 0x30, 0xb4, 0x09, 0x2e, 0xa9, 0x64, 0xa7, 0xf6, 0x92, 0x38, 0x67, 0xac, 0x1b, 0x06},
			{0x13, 0x1e, 0xbd, 0x61, 0x18, 0x6d, 0xbe, 0xce, 0x98, 0xee, 0x99, 0x8e, 0x8b, 0x40, 0x50, 0xd6, 0xe8, 0xdf, 0x6c, 0x51},
		},
		{
			{0xe4, 0xb4, 0xa4, 0xac, 0x1a, 0xc1, 0xee, 0x96, 0xab, 0xe0, 0x91, 0xf5, 0x05, 0xdf, 0x7d, 0xf4, 0x00, 0xfd, 0x83, 0x94},
			{0x59, 0xcb, 0x96, 0x67, 0x39, 0x2c, 0x55, 0x20, 0x10, 0x87, 0xe5, 0xf6, 0x35, 0x31, 0x34, 0xee, 0xb0, 0x6d, 0x9d, 0xec},
			{0x1e, 0x4e, 0xde, 0x2e, 0xaa, 0xd1, 0x3a, 0x8e, 0xf8, 0x17, 0xc1, 0x59, 0x88, 0xf1, 0x02, 0x95, 0x06, 0x53, 0xb5, 0x30},
			{0x49, 0xe1, 0x74, 0x16, 0x25, 0x1b, 0xa7, 0xd7, 0x3c, 0x7f, 0x8b, 0x73, 0x57, 0x95, 0x7e, 0x60, 0x1f, 0xfd, 0x9c, 0xe6},
			{0x3c, 0x03, 0xae, 0xac, 0xb6, 0xa5, 0x34, 0xe8, 0xf1, 0x91, 0x20, 0x13, 0xfd, 0xe3, 0xc0, 0x60, 0xed, 0x42, 0x8c, 0xf5},
			{0xc5, 0x26, 0xc7, 0xe1, 0x85, 0x20, 0xc2, 0x64, 0x36, 0xc9, 0xfe, 0x55, 0x8f, 0xb4, 0x25, 0x72, 0x62, 0x21, 0x80, 0xb6},
		},
		{
			{0x8f, 0xc7, 0xd6, 0x0c, 0x93, 0xf6, 0x31, 0x49, 0x1a, 0x73, 0xe2, 0x6f, 0xe3, 0xd0, 0xc4, 0x2c, 0x40, 0x54, 0x70, 0x6e},
			{0xe1, 0x20, 0xb9, 0xc2, 0xab, 0xeb, 0xcb, 0xb5, 0xea, 0x33, 0x8d, 0x93, 0x84, 0x46, 0xcf, 0xa0, 0xa8, 0x0e, 0x45, 0xed},
			{0xdc, 0x09, 0x03, 0xd3, 0x46, 0x37, 0x57, 0xab, 0x33, 0xe2, 0xbe, 0xd6, 0x01, 0x3d, 0x8f, 0x93, 0xaf, 0xa3, 0x70, 0xe5},
			{0x5a, 0x93, 0xf1, 0xa9, 0xf9, 0x7a, 0xb2, 0xf1, 0x05, 0xca, 0x9f, 0x8a, 0x2b, 0x7f, 0x9e, 0x6b, 0xa0, 0xf4, 0x85, 0x18},
			{0xb1, 0xc2, 0xca, 0x06, 0x6c, 0x5b, 0x5b, 0x9c, 0xd3, 0x50, 0xca, 0x17, 0xf5, 0x8e, 0x4e, 0xcd, 0x82, 0x5a, 0x1b, 0x57},
			{0x67, 0x8d, 0x0d, 0x8a, 0xa3, 0xf5, 0xa6, 0x64, 0x01, 0x7d, 0xf0, 0x43, 0xb1, 0x3c, 0xbb, 0xe8, 0x04, 0x53, 0x15, 0x0f},
		}
	};

	mDNSu32 c = 0;
	mDNSu8 digest_output[SHA1_OUTPUT_SIZE];

	for (size_t i = 0; i < sizeof(iterations) / sizeof(mDNSs32); i++) {
		mDNSu32 iteration = iterations[i];
		for (size_t j = 0; j < sizeof(salts) / sizeof(char *); j++) {
			const char * const salt = salts[j];
			for (size_t k = 0; k < sizeof(inputs) / sizeof(char *); k++) {
				const char * input = inputs[k];

				mDNSBool calculated = calculate_hash_for_nsec3(digest_output, SHA1_OUTPUT_SIZE, NSEC3_HASH_ALGORITHM_SHA_1,
					(unsigned char *)input, strlen(input), (unsigned char *)salt, strlen(salt), iteration);
				XCTAssertTrue(calculated);

				mDNSBool hash_matches = (memcmp(digest_output, outputs[c][k], SHA1_OUTPUT_SIZE) == 0);

				XCTAssertTrue(hash_matches, "c: %u, iteration: %u, salt: %s, input: %s", c, iteration, salt, input);
			}
			c++;
		}
	}
}

@end
#endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
